define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/productspec/index',
                    add_url: 'wu/productspec/add',
                    edit_url: 'wu/productspec/edit',
                    del_url: 'wu/productspec/del',
                    multi_url: 'wu/productspec/multi',
                    table: 'wu_productspec',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                sortOrder: "asc",
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'spectype_id', title: __('Spectype_id')},

                        {field: 'spectype.name', title: __('Spectype.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'specnumber', title: __('Specnumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'nbznumber', title: __('Nbznumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'lycl', title: __('Lycl'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'bycl', title: __('Bztb'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,

                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('產品製程'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-wrench',
                                    url: 'wu/procedure/index'
                                },
                                {
                                    name: 'addtabs',
                                    title: __('BOM'),
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-dropbox',
                                    url: 'wu/bomproductspec/index'
                                },
                                {
                                    name: 'addtabs',
                                    title: __('庫存'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-database',
                                    url: 'wu/stockproductspec/index'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },

                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'spectype.id', title: __('Spectype.id')},
                        //{field: 'spectype.create_time', title: __('Spectype.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'spectype.update_time', title: __('Spectype.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_wu_productspec', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_wu_productspec')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_productspec')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});